home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / ODBC for Macintosh / ODBC Tools / SampleSetup / Sources / ODBCdbmsSetup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-19  |  8.1 KB  |  326 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    ODBCdbmsSetup.c -- ODBC Sample DBMS Setup Shared Library (or DLL).
  3.  *
  4.  *    (c) Apple Computer, Inc 1993
  5.  */
  6.  
  7. #include <Dialogs.h>
  8. #include <Fonts.h>
  9. #include <LibraryManagerUtilities.h>
  10. #include <Menus.h>
  11. #include <QuickDraw.h>
  12. #include <StdIO.h>
  13. #include <String.h>
  14. #include <TextEdit.h>
  15. #include <Windows.h>
  16.  
  17. #include "DBMSsetup.h"
  18. #include "ODBCINST.H"
  19.  
  20. #include "ODBCdbmsSetup.h"
  21.  
  22. /*
  23.  *    Keywords
  24.  */
  25.  
  26. LPCSTR kDefaultDataSourceName    = "Default";        // default data source name
  27. LPCSTR kDataSourceNameKeyword    = "DSN";            // data source name keyword
  28. LPCSTR kDescriptionKeyword        = "Description";    // data source description keyword
  29. LPCSTR kOtherKeyword            = "Other";            // other keyword
  30. LPCSTR kTranslateKeyword        = "Translate";        // translate keyword
  31. LPCSTR kTranslateOptionKeyword    = "TranlsateOption";// translate option keyword
  32.  
  33. /*
  34.  *    Globals
  35.  */
  36.  
  37. TLibraryFile    *gLibraryFile;
  38.  
  39. /*
  40.  *    prototypes
  41.  */
  42.  
  43. INSTAPI    DoConfigDSN                        (WindowPtr parentWindow, UINT fRequest, LPCSTR lpszDriver, LPCSTR lpszAttributes);
  44. INSTAPI    RemoveDataSource                (WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info);
  45. INSTAPI    AddDataSource                    (WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info);
  46. INSTAPI    ConfigureDataSource                (WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info);
  47. INSTAPI    ParseAttributes                    (LPCSTR lpszAttributes, DataSourceInfo *info);
  48. INSTAPI    GetKeywordValueFromAttributes    (LPCSTR lpszKeyword, LPCSTR lpszAttributes, LPCSTR lpszString);
  49.  
  50. /*
  51.  *    public functions
  52.  */
  53.  
  54. void
  55. ODBCdbmsSetupInit()
  56. {
  57.     /*
  58.      *    Shared library init function
  59.      */
  60.     
  61.     GlobalWorld    saved;
  62.     
  63.     /*
  64.      *    switch to shared library's A5
  65.      */
  66.     
  67.     saved = OpenGlobalWorld();
  68.     
  69.     /*
  70.      *    save the local library file so we can quickly preflight later
  71.      */
  72.     
  73.     gLibraryFile = GetLocalLibraryFile();
  74.     if (gLibraryFile == NULL)
  75.     {
  76.         DebugStr("\p GetLocalLibraryFile failed");
  77.     }
  78.     
  79.     /*
  80.      *    since we may be posting dialogs the QuickDraw globals, et al. must be initialized
  81.      */
  82.     
  83.     InitGraf(&qd.thePort);
  84.     InitFonts();
  85.     InitWindows();
  86.     InitMenus();
  87.     TEInit();
  88.     InitDialogs(NULL);
  89.     
  90.     /*
  91.      *    always remember to restore original A5
  92.      */
  93.     
  94.     CloseGlobalWorld(saved);
  95. }
  96.  
  97. void
  98. ODBCdbmsSetupCleanUp()
  99. {
  100.     /*
  101.      *    Shared library cleanup function
  102.      */
  103. }
  104.  
  105. INSTAPI
  106. ConfigDSN(GrafPtr    pGrafPort,            /* window to center over                    */
  107.           UINT        fRequest,            /* add, modify or delete                    */
  108.           LPCSTR    lpszDriver,            /* name to present to user for this driver    */
  109.           LPCSTR    lpszAttributes)        /* buffer holding section data                */
  110. {
  111.     /*
  112.      *    Shared lilbrary entry point for configuring a data source.
  113.      */
  114.     
  115.     INSTAPI            success;
  116.     long            savedrefnum;
  117.     OSErr            err;
  118.     GlobalWorld        saved;
  119.     
  120.     /*
  121.      *    setup for using shared library globals, resources
  122.      */
  123.     
  124.     saved = OpenGlobalWorld();
  125.     if( err = Preflight( gLibraryFile, &savedrefnum ) ) 
  126.     {
  127.         DebugStr("\p Preflight failed");
  128.         CloseGlobalWorld(saved);
  129.         return FALSE;
  130.     }
  131.     
  132.     /*
  133.      *    here's where the work gets done
  134.      */
  135.     
  136.     success = DoConfigDSN((WindowPtr) pGrafPort, fRequest, lpszDriver, lpszAttributes);
  137.     
  138.     /*
  139.      *    restore resource path, A5 world
  140.      */
  141.     
  142.     Postflight(gLibraryFile, savedrefnum);
  143.     CloseGlobalWorld(saved);
  144.     
  145.     return success;
  146. }
  147.  
  148. /*
  149.  *    private functions
  150.  */
  151.  
  152. static INSTAPI
  153. DoConfigDSN(WindowPtr parentWindow, UINT fRequest, LPCSTR lpszDriver, LPCSTR lpszAttributes)
  154. {
  155.     /*
  156.      *    Pull out the keyword values from the attribute buffer, then add, configure or
  157.      *    remove the data source. note that the data source name must be one of the
  158.      *    keyword values in the buffer (DSN=Data Source Name), or nothing can be done.
  159.      */
  160.     
  161.     INSTAPI            success = false;
  162.     DataSourceInfo    info;
  163.     
  164.     if (!ParseAttributes(lpszAttributes, &info)) return false;
  165.     
  166.     switch (fRequest)
  167.     {
  168.         case ODBC_ADD_DSN:
  169.             success = AddDataSource(parentWindow, lpszDriver, &info);
  170.             break;
  171.         
  172.         case ODBC_CONFIG_DSN:
  173.             success = ConfigureDataSource(parentWindow, lpszDriver, &info);
  174.             break;
  175.         
  176.         case ODBC_REMOVE_DSN:
  177.             success = RemoveDataSource(parentWindow, lpszDriver, &info);
  178.             break;
  179.     }
  180.     
  181.     return success;
  182. }
  183.  
  184. static INSTAPI
  185. RemoveDataSource(WindowPtr /*parentWindow*/, LPCSTR /*lpszDriver*/, DataSourceInfo *info)
  186. {
  187.     /*
  188.      *    Call the config manager to remove the data source from the ODBC prefs file
  189.      */
  190.     
  191.     return SQLRemoveDSN(info->name);
  192. }
  193.  
  194. static INSTAPI
  195. AddDataSource(WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info)
  196. {
  197.     /*
  198.      *    Call the setup dialog on the data source info, then call the config manager to 
  199.      *    update the data source entry in the ODBC prefs file if the user clicked OK. Make
  200.      *    sure to remove the old data source entry as the user may have changed the name.
  201.      */
  202.     
  203.     if (!DoDBMSsetup(parentWindow, info))
  204.         return false;
  205.     
  206.     /*
  207.      *    might want to add check that datasource doesn't already exist before writing!?
  208.      */
  209.     
  210.     if (!SQLWriteDSN(info->name, lpszDriver)) return false;
  211.     
  212.     if (!SQLSetKeywordValue(info->name, kDescriptionKeyword,
  213.         info->description, strlen(info->description))) return false;
  214.     if (!SQLSetKeywordValue(info->name, kOtherKeyword,
  215.         info->other, strlen(info->other))) return false;
  216.     if (!SQLSetKeywordValue(info->name, kTranslateKeyword,
  217.         info->translate, strlen(info->translate))) return false;
  218.     if (!SQLSetKeywordValue(info->name, kTranslateOptionKeyword,
  219.         info->translateOption, strlen(info->translateOption))) return false;
  220.     
  221.     return true;
  222. }
  223.  
  224. static INSTAPI
  225. ConfigureDataSource(WindowPtr parentWindow, LPCSTR lpszDriver, DataSourceInfo *info)
  226. {
  227.     /*
  228.      *    Call the setup dialog on the data source info, then call the config manager to 
  229.      *    update the data source entry in the ODBC prefs file if the user clicked OK. Make
  230.      *    sure to remove the old data source entry as the user may have changed the name.
  231.      */
  232.     
  233.     char    oldName[kMaxValueLength];
  234.     
  235.     strcpy(oldName, info->name);
  236.     
  237.     if (!DoDBMSsetup(parentWindow, info))
  238.         return false;
  239.     
  240.     if (!SQLRemoveDSN(oldName))
  241.         return false;
  242.     
  243.     if (!SQLWriteDSN(info->name, lpszDriver))
  244.         return false;
  245.     
  246.     if (!SQLSetKeywordValue(info->name, kDescriptionKeyword,
  247.         info->description, strlen(info->description))) return false;
  248.     if (!SQLSetKeywordValue(info->name, kOtherKeyword,
  249.         info->other, strlen(info->other))) return false;
  250.     if (!SQLSetKeywordValue(info->name, kTranslateKeyword,
  251.         info->translate, strlen(info->translate))) return false;
  252.     if (!SQLSetKeywordValue(info->name, kTranslateOptionKeyword,
  253.         info->translateOption, strlen(info->translateOption))) return false;
  254.     
  255.     return true;
  256. }
  257.  
  258. static INSTAPI
  259. ParseAttributes(LPCSTR lpszAttributes, DataSourceInfo *info)
  260. {
  261.     /*
  262.      *    Pull out the keyword values from the data source attributes buffer.
  263.      *    If the data source name keyword, DSN, isn't found, or is zero-length, then
  264.      *    return false. All other keyword values may be absent or zero-length.
  265.      */
  266.     
  267.     if (!GetKeywordValueFromAttributes(kDataSourceNameKeyword, lpszAttributes, info->name))
  268.         return false;
  269.     
  270.     GetKeywordValueFromAttributes(kDescriptionKeyword,        lpszAttributes, info->description);
  271.     GetKeywordValueFromAttributes(kOtherKeyword,            lpszAttributes, info->other);
  272.     GetKeywordValueFromAttributes(kTranslateKeyword,        lpszAttributes, info->translate);
  273.     GetKeywordValueFromAttributes(kTranslateOptionKeyword,    lpszAttributes, info->translateOption);
  274.     
  275.     return true;
  276. }
  277.  
  278. static INSTAPI
  279. GetKeywordValueFromAttributes(LPCSTR lpszKeyword, LPCSTR lpszAttributes, LPCSTR lpszString)
  280. {
  281.     /*
  282.      *    searches the attributes buffer (containing the packed array of keyword=value null-terminated
  283.      *    strings, with an additional null terminating the entire array) for the requested keyword,
  284.      *    and copies its value over to lpszString. lpszString should point to a buffer at least 
  285.      *    kMaxValueLength bytes long. returns true if found and non-zero, false otherwise.
  286.      */
  287.     
  288.     char    *s, *t, *u;
  289.     char    tmpKeyword[kMaxKeywordLength];
  290.     
  291.     /*
  292.      *    loop through each keyword=value string in the buffer
  293.      */
  294.     
  295.     for (lpszString[0] = 0, s = lpszAttributes; *s; s += strlen(s) + 1)
  296.     {
  297.         /*
  298.          *    pull out the keyword as a c-string
  299.          */
  300.         
  301.         for (u = s, t = tmpKeyword; *u; u++, t++)
  302.         {
  303.             if (*u == '=')
  304.             {
  305.                 u++;
  306.                 break;
  307.             }
  308.             *t = *u;
  309.         }
  310.         *t = 0;
  311.         
  312.         /*
  313.          *    if this is the desired keyword then copy its value to lpszString and beat it. u was
  314.          *    left pointing to the start of the value string from the previous operation
  315.          */
  316.         
  317.         if (strcmp(tmpKeyword, lpszKeyword) == 0)
  318.         {
  319.             strcpy(lpszString, u);
  320.             return true;
  321.         }
  322.     }
  323.     
  324.     return false;
  325. }
  326.